package yy.com.yjbotouch.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import yy.com.yjbotouch.R;


public class MyCustomLayout extends ViewGroup {

    private int mHorizontalSpacing;
    private int mVerticalSpacing;


    public MyCustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomLayout);
        mHorizontalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_horizontal_spacing,
                getResources().getDimensionPixelSize(R.dimen.horizontal_spacing));
        mVerticalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_vertical_spacing,
                getResources().getDimensionPixelSize(R.dimen.vertical_spacing));
        ta.recycle();
    }

    //return requestDisallowInterceptTouchEvent(true);


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("yjbo-MyCust-onInt-",ev.getAction()+"--"+super.onInterceptTouchEvent(ev));
        return super.onInterceptTouchEvent(ev);
//        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("yjbo-MyCust-onTou-",event.getAction()+"--"+super.onTouchEvent(event));
        return super.onTouchEvent(event);
//        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d("yjbo-MyCust-dispa-",ev.getAction()+"--"+super.onTouchEvent(ev));
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        // TODO Auto-generated method stub
        return p != null;
    }


    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        // TODO Auto-generated method stub
        return (LayoutParams) p;
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }


    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 0;
        int height = getPaddingTop();
        int verticalSpacing;
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            verticalSpacing = mVerticalSpacing;
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp.verticalSpacing > 0) {
                verticalSpacing += lp.verticalSpacing;
            }
            width = getPaddingLeft() + mHorizontalSpacing * i;
            lp.x = width;
            lp.y = height;
            width += child.getMeasuredWidth();
            height += verticalSpacing;
        }
        width += getPaddingRight();
        height += getChildAt(getChildCount() - 1).getMeasuredHeight() + getPaddingBottom();

        setMeasuredDimension(resolveSize(width, widthMeasureSpec),
                resolveSize(height, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
        }
    }

    public static class LayoutParams extends ViewGroup.LayoutParams {
        public int x;
        public int y;
        public int verticalSpacing;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.MyCustomLayout_LayoutParams);
            verticalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_LayoutParams_layout_vertical_spacing, -1);
            ta.recycle();
        }

        public LayoutParams(int w, int h) {
            super(w, h);
        }
    }


}